<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="10" :lg="11" :md="12" :sm="24">
            <a-form-item :label="$t('订单时间')">
              <j-date
                :show-time="{ defaultValue: moment('00:00:00', 'HH:mm:ss') }"
                valueFormat="YYYY-MM-DD HH:mm:ss"
                dateFormat="YYYY-MM-DD HH:mm:ss"
                :placeholder="$t('请选择开始时间')"
                class="query-group-cust"
                v-model="queryParam.createTime_begin"
                style="min-width: calc(50% - 10px)"
              ></j-date>
              <span class="query-group-split-cust"></span>
              <j-date
                :show-time="{ defaultValue: moment('23:59:59', 'HH:mm:ss') }"
                valueFormat="YYYY-MM-DD HH:mm:ss"
                dateFormat="YYYY-MM-DD HH:mm:ss"
                :placeholder="$t('请选择结束时间')"
                class="query-group-cust"
                v-model="queryParam.createTime_end"
                style="min-width: calc(50% - 10px)"
              ></j-date>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item :label="$t('业务类型')">
              <a-select v-model="queryParam.typeId" :placeholder="$t('请选择业务类型')">
                <a-select-option value="">{{ $t('全部') }}</a-select-option>
                <a-select-option :value="item.value" :key="index" v-for="(item, index) in typeList">{{
                  item.title
                }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item :label="$t('会员手机号')">
                <a-input :placeholder="$t('请输入会员手机号')" v-model="queryParam.memberPhone"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item :label="$t('会员姓名')">
                <a-input :placeholder="$t('请输入会员姓名')" v-model="queryParam.memberName"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item :label="$t('订单金额')">
                <a-input
                  :placeholder="$t('请输入最小值')"
                  class="query-group-cust"
                  v-model="queryParam.price_begin"
                ></a-input>
                <span class="query-group-split-cust"></span>
                <a-input
                  :placeholder="$t('请输入最大值')"
                  class="query-group-cust"
                  v-model="queryParam.price_end"
                ></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item :label="$t('订单状态')">
                <j-dict-select-tag
                  :placeholder="$t('请选择订单状态')"
                  v-model="queryParam.state"
                  dictCode="bis_order.state"
                />
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item :label="$t('订单号')">
                <j-input :placeholder="$t('请输入订单号')" v-model="queryParam.id"></j-input>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item :label="$t('关联订单号')">
                <j-input :placeholder="$t('请输入关联订单号')" v-model="queryParam.subOrderId"></j-input>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item :label="$t('场地订单号')">
                <j-input :placeholder="$t('请输入场地订单号')" v-model="queryParam.transOrderNo"></j-input>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24" v-if="!tenantid">
              <a-form-item :label="$t('门店')">
                <a-select v-model="queryParam.yardCode">
                  <a-select-option v-for="(item, i) in shops" :key="i" :value="item.value"
                    >{{ item.title }}<span v-if="item.value != ''">（{{ item.value }}）</span></a-select-option
                  >
                </a-select>
              </a-form-item>
            </a-col>
          </template>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">{{ $t('查询') }}</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">{{
                $t('重置')
              }}</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? $t('收起') : $t('展开') }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'" />
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <!-- <a-button type="primary" icon="download" @click="handleExportXls('业务订单')">导出</a-button> -->
    </div>

    <!-- table区域-begin -->
    <div>
      <a-table
        ref="table"
        size="middle"
        :scroll="{ x: true }"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        class="j-table-force-nowrap"
        @change="handleTableChange"
      >
        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px; font-style: italic">{{ $t('无图片') }}</span>
          <img
            v-else
            :src="getImgView(text)"
            height="25px"
            alt
            style="max-width: 80px; font-size: 12px; font-style: italic"
          />
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px; font-style: italic">{{ $t('无文件') }}</span>
          <a-button v-else :ghost="true" type="primary" icon="download" size="small" @click="downloadFile(text)">{{
            $t('下载')
          }}</a-button>
        </template>
        <template slot="address" slot-scope="text, record">
          <div style="white-space: normal">
            <div v-if="text">{{ record.mallAddress.name }} {{ record.mallAddress.phone }}</div>
            <div v-if="text">{{ record.mallAddress.region + record.mallAddress.addressDetail }}</div>
          </div>
        </template>
        <span slot="action" slot-scope="text, record">
          <a v-has="'GiftOrderController:edit'" v-if="record.typeId == 3" @click="handleLogistics(record)">{{ $t('物流') }}</a>
        </span>
        <template slot="footer">
          <div style="font-weight: bold" v-if="totalInfo">{{ totalInfo }}</div>
        </template>
      </a-table>
    </div>
    <logistics-modal ref="logisticsModal" @ok="modalFormOk"></logistics-modal>
  </a-card>
</template>

<script>
import Vue from 'vue'
import moment from 'moment'
import '@/assets/less/TableExpand.less'
import { getAction } from '@/api/manage'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { initDictOptions } from '@/components/dict/JDictSelectUtil'
import LogisticsModal from './modules/LogisticsModal'

export default {
  name: 'OrderList',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    LogisticsModal,
  },
  data() {
    return {
      description: '业务订单管理页面',
      moment,
      // 表头
      columns: [
        {
          title: this.$t('订单号'),
          dataIndex: 'id',
          align: 'center',
        },
        {
          title: this.$t('订单时间'),
          align: 'center',
          dataIndex: 'createTime',
        },
        {
          title: this.$t('业务类型'),
          align: 'center',
          dataIndex: 'typeName',
        },
        {
          title: this.$t('会员手机号'),
          align: 'center',
          dataIndex: 'memberPhone',
        },
        {
          title: this.$t('会员姓名'),
          align: 'center',
          dataIndex: 'memberName',
        },
        {
          title: this.$t('支付方式'),
          align: 'center',
          dataIndex: 'payType_dictText',
        },
        {
          title: this.$t('订单金额'),
          align: 'right',
          dataIndex: 'cashPrice',
          customRender: (text, r) => {
            text = (text ==null || text =="" || text ==undefined)?0:parseFloat(text);
            return text.toFixed(2);
          }
        },
        {
          title: this.$t('兑换值'),
          align: 'right',
          dataIndex: 'price',
          customRender: (text, r) => {
            text = (text ==null || text =="" || text ==undefined)?0:parseFloat(text);
            return text.toFixed(2);
          }
        },
        {
          title: this.$t('订单状态'),
          align: 'center',
          dataIndex: 'state_dictText',
        },
        {
          title: this.$t('关联订单号'),
          align: 'center',
          dataIndex: 'subOrderId',
        },
        {
          title: this.$t('场地订单号'),
          align: 'center',
          dataIndex: 'transOrderNo',
        },
        /* {
          title: this.$t('备注'),
          align: 'center',
          dataIndex: 'comment'
        }, */
        {
          title: this.$t('收货地址'),
          align: 'center',
          dataIndex: 'address',
          customCell: (r, i) => {
            return {
              style: {
                textAlign: 'left',
                maxWidth: '140px',
              },
            }
          },
          scopedSlots: { customRender: 'address' },
        },
        {
          title: this.$t('操作'),
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' },
        },
      ],
      url: {
        list: '/gift/order',
      },
      typeList: [],
      totalCallback: this.totalCallbacks,
      shops: [],
      tenantid: Vue.ls.get('TENANT_ID'),
      queryParam: {
        yardCode: '',
      },
      totalInfo:''
    }
  },
  created() {},
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    },
  },
  methods: {
    initDictConfig() {
      if (!this.tenantid) {
        this.columns.splice(0,0,{
          title: this.$t('门店'),
          align: 'left',
          dataIndex: 'yardCode',
          customRender: (t, r) => {
            let str
            if (t == 0) {
              str = this.$t('总部')
            } else {
              str = r.yardName ? t + ' ' + r.yardName : t
            }
            return str
          },
        })
        initDictOptions('bis_shop,name,code').then((res) => {
          if (res.success) {
            this.shops = res.result
            this.shops.unshift({ value: '', title: this.$t('全部') })
          }
        })
      }
      getAction('gift/dict', { dictCode: 'bis_order.type' }).then((res) => {
        if (res.success) {
          this.typeList = res.result
        }
      })
    },
    handleLogistics(record) {
      this.$refs.logisticsModal.show(record)
    },
    totalCallbacks(totalInfo) {
      if (this.dataSource.length == 0) {
        this.totalInfo = this.$t('合计') + '：--'
        return
      }
      //合计
      this.totalInfo =
        this.$t('合计') +
        '：' +
        this.$t('订单金额') +
        '：' +
        totalInfo.cashPrice.toFixed(2) +
        '，' +
        this.$t('兑换值') +
        '：' +
        totalInfo.price.toFixed(2)
    },
    searchReset() {
      this.queryParam = {
        yardCode:''
      }
      this.searchQuery()
    },
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>